package com.healthcloud.service.patient.impl;

import com.healthcloud.dao.patient.PatientDataDao;
import com.healthcloud.dto.res.CountSpecsRes;
import com.healthcloud.entity.patient.PatientDataEntity;
import com.healthcloud.service.patient.PatientDataService;
import com.healthcloud.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("patientDataService")
@Transactional
public class PatientDataServiceImpl implements PatientDataService {
    @Autowired
    private PatientDataDao patientDataDao;

    @Override
    public PatientDataEntity queryObject(Integer id) {
        return patientDataDao.queryObject(id);
    }

    @Override
    public List<PatientDataEntity> queryList(PatientDataEntity bean, PageParam pageParam) {
        return patientDataDao.queryList(bean, pageParam);
    }

    @Override
    public int queryTotal(PatientDataEntity bean) {
        return patientDataDao.queryTotal(bean);
    }

    @Override
    public void save(PatientDataEntity patientData) {
        patientDataDao.save(patientData);
    }

    @Override
    public void update(PatientDataEntity patientData) {
        patientDataDao.update(patientData);
    }

    @Override
    public void delete(Integer id) {
        patientDataDao.delete(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        patientDataDao.deleteBatch(ids);
    }

    @Override
    public void saveOrUpdate(PatientDataEntity entity) {
        PatientDataEntity data = queryByIdCard(entity.getIdCard());
        entity.setUpdateTime(new Date());
        if (data == null) {
            entity.setCreateTime(new Date());
            save(entity);
        } else {
            update(entity);
        }
    }

    @Override
    public PatientDataEntity queryByIdCard(String idCard) {
        return patientDataDao.queryByIdCard(idCard);
    }

    @Override
    public String getSpecRemarkByIdCard(String idCard) {
        return patientDataDao.getSpecRemarkByIdCard(idCard);
    }

    @Override
    public List<CountSpecsRes> countSpecsLastMonth(String ids) {
        return patientDataDao.countSpecsLastMonth(ids);
    }

    @Override
    public List<CountSpecsRes> countSpecsPerMonth(String ids, String year) {
        return patientDataDao.countSpecsPerMonth(ids,year);
    }

    /**
     * 修改患者数据的特殊处理备注
     *
     * @param data
     * @return
     */
    @Override
    public Boolean updateSpecRemark(PatientDataEntity data) {
        return patientDataDao.updateSpecRemark(data) > 0;
    }

    /**
     * 根据身份证号，查询最近7天是否未测量过血压
     * @param idCard 身份证号
     * @return true 代表未测量过，false 代表没有绑定仪器或已测量过
     */
    @Override
    public boolean queryNotBpTestDataSevenDays(String idCard) {
        // 判断患者是否已绑定仪器：血压计
        Integer bindCount = patientDataDao.getBpDeviceBindStatus(idCard);
        // 判断患者仪器最近 7 天是否有测量数据
        Integer testDataCount = patientDataDao.getBpTestDataSevenDays(idCard);
        if (bindCount > 0 && testDataCount == 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据身份证号，查询最近7天是否未测量过血糖
     * @param idCard 身份证号
     * @return true 代表未测量过，false 代表没有绑定仪器或已测量过
     */
    @Override
    public boolean queryNotBgTestDataSevenDays(String idCard) {
        // 判断患者是否已绑定仪器：血糖仪
        Integer bindCount = patientDataDao.getBgDeviceBindStatus(idCard);
        // 判断患者仪器最近 7 天是否有测量数据
        Integer testDataCount = patientDataDao.getBgTestDataSevenDays(idCard);
        if (bindCount > 0 && testDataCount == 0) {
            return true;
        }
        return false;
    }

}